package project.ccso.top.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import project.ccso.top.dao.ProvinceDao;
import project.ccso.top.entity.Province;
import project.ccso.top.util.DBUtil;

/**
 * 省份Dao的实现类
 * 
 * @author aibinxiao
 * @date 2018年8月23日 上午10:41:30
 */
public class ProvinceDaoImpl implements ProvinceDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(ProvinceDaoImpl.class);
	private DBUtil dbUtil = new DBUtil();

	@Override
	public int countByParams(String id, String name, String startDate, String endDate) {
		List<Object> objects = new ArrayList<>();
		int count = 0;
		String sql = "select count(*) from t_province where 1=1";
		if (!StringUtils.isEmpty(id)) {
			sql += " and province_id = ?";
			objects.add(id);
		}
		if (!StringUtils.isEmpty(name)) {
			sql += " and province_name = ?";
			objects.add(name);
		}
		if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			sql += " and province_create_time between ? and ?";
		}
		ResultSet rs = dbUtil.executeQuery(sql, objects);
		try {
			while (rs.next()) {
				count = rs.getInt(1);
			}
			logger.info("找到(" + count + ")个符合查询条件的省份信息");
			return count;
		} catch (SQLException e) {
			logger.info("没有找到符合查询条件求的省份信息");
			e.printStackTrace();
			return count;
		}
	}

	/**
	 * 分页查询符合条件的省份记录Dao实现类
	 */
	@Override
	public List<Province> findPageByParams(String id, String name, String startDate, String endDate, int startIndex,
			int records) {
		List<Province> result = new ArrayList<>();
		List<Object> objects = new ArrayList<>();
		String sql = "select * from t_province where 1=1 ";

		if (!StringUtils.isEmpty(id)) {
			sql += " and province_id = ?";
			objects.add(id);
		}
		if (!StringUtils.isEmpty(name)) {
			sql += " and province_name = ?";
			objects.add(name);
		}
		if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			sql += " and province_create_time between ? and ?";
		}
		sql += " limit ? , ?";
		objects.add(startIndex);
		objects.add(records);
		ResultSet rs = dbUtil.executeQuery(sql, objects);
		try {
			while (rs.next()) {
				Province province = new Province(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4),
						rs.getString(5));
				result.add(province);
			}
		} catch (SQLException e) {
			result = null;
		}
		return result;
	}

	/**
	 * 根据Id查找省份*
	 */
	@Override
	public Province findById(int id) {
		Province province = null;
		String sql = "select * from t_province where province_id = ?";
		ResultSet rs = dbUtil.executeQuery(sql, new Object[] {id});
		try {
			if (rs.next()) {
				province = new Province(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4), rs.getString(5));
			}
		} catch (SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return province;
	}
	
	/**
	 *  根据Id修改省份*
	 */
	@Override
	public boolean saveOrUpdateById(String idStr, String provinceName) {
		boolean flag = false;
		String sql = "";
		int count = 0;
		if(StringUtils.isEmpty(idStr)) {
			// 添加操作
			sql = "insert into t_province (province_name, province_status, province_create_time, province_alter_time) values (?,?,?,?)";
			count = dbUtil.executeUpdate(sql, new Object[] {provinceName, 0, new Date(), new Date()});
			if(count != -1) {
				flag = true;
				logger.info("provinceName为" + provinceName + "的省份信息添加成功！");
			}
		}else {
			// 修改操作
			int id = 1;
			try {
				id = Integer.parseInt(idStr);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("参数id不是整数类型或超出int范围！");
			}
			sql = "update t_province set province_name = ? , province_alter_time = ? where province_id = ?";
			count = dbUtil.executeUpdate(sql, new Object[] {provinceName, new Date(), id});
			if(count != -1) {
				flag = true;
				logger.info("id为" + id + "的省份信息修改成功！");
			}
		}
		return flag;
	}

	/**
	 * 根据省份id修改省份信息或根据provinceName插入城市信息的Dao的实现
	 * *
	 */
	@Override
	public List<Province> selectAllProvince() {
		List<Province> list = new ArrayList<>();
		String sql = "select * from t_province";
		ResultSet rs = dbUtil.executeQuery(sql);
		try {
			while(rs.next()) {
				list.add(new Province(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5)));
			}
			logger.info("查询省份信息成功，共"+list.size()+"个省份！");
		} catch (SQLException e) {
			list = null;
			e.printStackTrace();
			logger.error("省份查询内容为空");
		}
		return list;
	}

	@Override
	public boolean updateShieldStatus(int id, int transStatus) {
		boolean flag = false;
		int count = 0;
		String sql = "update t_province set province_status = ? where province_id = ?";
		count = dbUtil.executeUpdate(sql, new Object[] {transStatus, id});
		if(count != -1) {
			flag = true;
			if(transStatus == 1) {
				logger.info("id为" + id + "的省份启用成功！");
			}else if(transStatus == 0) {
				logger.info("id为" + id + "的省份启用成功！");
			}
		}
		return flag;
	}

	/**
	 * 5. 更新或者保存省份信息的Dao实现类
	 */
	/*
	 * @Override public void saveProvince(Province province) throws Exception {
	 * this.getHibernateTemplate().saveOrUpdate(province); }
	 * 
	 *//**
		 * 6.根据id和状态修改省份信息的启用状态Dao实现类
		 *//*
			 * @Override public void updateStauts(Integer id, Integer statusCode) throws
			 * Exception { String hql = "from Province where id = ?"; List<Province> list =
			 * this.getHibernateTemplate().find(hql, id); if(list!=null && list.size()>0){
			 * Province province = list.get(0); province.setStatus(statusCode);
			 * province.setAlterTime(DateUtils.DateToStr(new Date()));
			 * this.getHibernateTemplate().saveOrUpdate(province); } }
			 */
}
